/**
 * 各种格式化
 * */
layui.define((exports) => {
	let sdFormat = {
		// 生成查询条件
		query: function(where, val) {
			let arr = [];
			$.each(where, function(index, item) {
				if (val) {
					// 先判断默认值 再去取val
					if ((typeof item.field) != 'undefined') {
						if (item.val == -1 || item.val == '') {
							if ((typeof val[item.field]) != 'undefined') {
								if (val[item.field] == -1 || val[item.field] == '') {

								} else {
									if (item.opt == 'between') {
										let d = val[item.field].split(" ");
										let s = d[0] + ' 00/00/01';
										let e = d[2] + ' 23/59/59';
										let tem = item.field + ':' + item.opt + ':' + s + ':' + e;
										arr.push(tem);
									} else {
										let tem = item.field + ':' + item.opt + ':' + val[item.field];
										arr.push(tem);
									}
								}
							}
						} else {
							if (item.opt == 'between') {
								let d = val[item.field].split(" ");
								let s = d[0] + ' 00/00/01';
								let e = d[2] + ' 23/59/59';
								let tem = item.field + ':' + item.opt + ':' + s + ':' + e;
								arr.push(tem);
							} else {
								let tem = item.field + ':' + item.opt + ':' + val[item.field];
								arr.push(tem);
							}
						}
					}
				} else {
					if ((typeof item.field) != 'undefined') {
						if (item.val == -1 || item.val == '') {

						} else {
							if (item.opt == 'between') {
								let d = (item.val).split(" ");
								let s = d[0] + ' 00/00/01';
								let e = d[2] + ' 23/59/59';
								let tem = item.field + ':' + item.opt + ':' + s + ':' + e;
								arr.push(tem);
							} else {
								let tem = item.field + ':' + item.opt + ':' + item.val;
								arr.push(tem);
							}
						}
					}
				}
			});
			if (arr.length > 0) {
				return arr.join('~');
			} else {
				return null;
			}
		},
		// 生成排序条件
		orderBy: function(orderBy) {
			let arr = [];
			$.each(orderBy, function(index, item) {
				let tem = item.field + ':' + item.orderBy;
				arr.push(tem);
			});
			if (arr.length > 0) {
				return arr.join('~');
			} else {
				return null;
			}
		},
		// json处理
		json: {
		    update(_array, _obj, _arrt){
		        $.each(_array, function (idx, obj) {
		            let attr = _arrt || 'id';
		            if(obj[attr] = _obj[attr]){
		                obj = _obj;
		            }
		        })
		        return _array;
		    },
		    o2j: function(object) {
		        if (typeof object == "string") {
		            try {
		                let q = JSON.parse(object);
		                if (typeof q == "object" && q) {
		                    return q
		                }
		            } catch (p) {}
		        }
		    },
		    syntaxHighlight: function(json) {
		        if (typeof json != 'string') {
		            json = JSON.stringify(json, undefined, 2);
		        }
		        json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
		        return '<pre id="result">' + json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) {
		            let cls = 'number';
		            if (/^"/.test(match)) {
		                if (/:$/.test(match)) {
		                    cls = 'key';
		                } else {
		                    cls = 'string';
		                }
		            } else if (/true|false/.test(match)) {
		                cls = 'boolean';
		            } else if (/null/.test(match)) {
		                cls = 'null';
		            }
		            return '<span class="' + cls + '">' + match + '</span>';
		        }) + '</pre>';
		    },
		    o2s(_obj) {
		        return JSON.stringify(_obj);
		    },
		    remove(_obj, _key, _filed) {
		        let tems = [];
		        $.each(_obj, function (idx, obj) {
		            let $id = (typeof _filed) == "undefined" ? 'id' : _filed;
		            if (obj[$id] != _key) {
		                tems.push(obj);
		            }
		        })
		        return tems;
		    },
		    get(_obj, _val, _filed) {
		        let tem = {};
		        $.each(_obj, function (idx, obj) {
		            let $id = (typeof _filed) == "undefined" ? 'id' : _filed;
		            if (obj[$id] == _val) {
		                tem = obj;
		            }
		        })
		        return tem;
		    },
		    toForm(_form) {
		        for (let v in _form) {
		            if ((typeof _form[v]) == 'number') {
		                _form[v] = _form[v].toString();
		            }
		        }
		        return _form;
		    },
		    getVal(_list, _filed) {
		        let tem = [];
		        $.each(_list, function (idx, obj) {
		            let $id = (typeof _filed) == "undefined" ? 'id' : _filed;
		            tem.push(obj[$id])
		        })
		        return tem;
		    },
		    tree2list: function(_tree, _key){
		        return _tree.reduce(function(con, item) {
		            let callee = arguments.callee;
		            con.push(item);
		            if (item[_key] && item[_key].length >0){
		                item[_key].reduce(callee, con);
		            }
		            return con;
		        }, [])
		            .map(function(item){
		                item[_key] = [];
		                return item;
		            })
		    },
		    list2tree: function(_list, _key){
		        let tree = _list.filter(function(parent) {
		            let branchArr = _list.filter(function(child) {
		                return parent.id == child[_key];
		            });
		            parent.nodes = [];
		            if (branchArr.length > 0) {
		                parent.nodes = branchArr;
		            }
		            return parent[_key] == "";
		        });
		        return tree;
		    },
		    l2f: function(_list, _formatter){
		        let tems = [];
		        $.each(_list, function (idx, obj){
		            for(var key in obj){
		                
		                $.each(_formatter, function (idx2, obj2){
		                    console.log(obj2.fun)
		                    if(key == obj2.filed){
		                        let tem = obj[key];
		                        obj[key] = obj2.fun(tem);
		                    }
		                })
		                tems.push(obj);
		            }
		        })
		        return tems;
		    }
		},
		// 字符串处理
		string: {
		    // 生成uudi
		    guid() {
		        return 'xxxxxxxx-xxxx-txxx-jxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
		            let r = Math.random() * 16 | 0,
		                v = c == 'x' ? r : (r & 0x3 | 0x8);
		            return v.toString(16);
		        });
		    },
		    // 补全0(>10)
		    time(_t) {
		        let t = parseInt(_t);
		        if(t < 10){
		            t = '0' + t;
		        }
		        return t;
		    },
		    formatCode(_txt) {
		        let code = 0;
		        // 1text 2number 3date 4textarea 5radio 6multiple 7edit 8image 9images 10file 11password
		        switch (_txt) {
		            case 'text':
		                code = 1;
		                break;
		            case 'number':
		                code = 2;
		                break;
		            case 'date':
		                code = 3;
		                break;
		            case 'textarea':
		                code = 4;
		                break;
		            case 'radio':
		                code = 5;
		                break;
		            case 'multiple':
		                code = 6;
		                break;
		            case 'edit':
		                code = 7;
		                break;
		            case 'image':
		                code = 8;
		                break;
		            case 'images':
		                code = 9;
		                break;
		            case 'file':
		                code = 10;
		                break;
		            case 'password':
		                code = 11;
		                break;
		        }
		        return code;
		    }
		},
		
		// 转换
		convert: {
			s2n(_s){
				let n = 0;
				if(!isNaN(parseInt(_s))){
					n = parseInt(_s);
				}
				return n;
			},
		},
		time: {
		    // 字符串转数字
		    s2n(_s){
		        let n = 0;
		        if(!isNaN(parseInt(_s))){
		            n = parseInt(_s);
		        }
		        return n;
		    },
		    // 时间戳转日期
		    t2t(_timestamp) {
		        _timestamp = (typeof _timestamp) == 'string' ? Number(_timestamp) : _timestamp;
		        if (_timestamp > 0) {
		            let d = null;
		            if((_timestamp.toString()).length == 13){
		                d = new Date(_timestamp);
		                
		            }else{
		                d = new Date(_timestamp * 1000);  
		            }
		          
		            let date = (d.getFullYear()) + "-" +
		                sdFormat.string.time((d.getMonth() + 1)) + "-" +
		                sdFormat.string.time(d.getDate()) + " " +
		                sdFormat.string.time(d.getHours()) + ":" +
		                sdFormat.string.time(d.getMinutes()) + ":" +
		                sdFormat.string.time(d.getSeconds());
		            return date;
		        } else {
		            return '';
		        }
		    },
		    t2s(_timestamp) {
		        _timestamp = (typeof _timestamp) == 'string' ? Number(_timestamp) : _timestamp;
		        if (_timestamp > 0) {
		            let d = null;
		            if((_timestamp.toString()).length == 13){
		                d = new Date(_timestamp);
		                
		            }else{
		                d = new Date(_timestamp * 1000);  
		            }
		          
		            let date = (d.getFullYear()) + "-" +
		                utils.string.time((d.getMonth() + 1)) + "-" +
		                utils.string.time(d.getDate());
		            return date;
		        } else {
		            return '';
		        }
		    },
		    t2m(_timestamp) {
		        _timestamp = (typeof _timestamp) == 'string' ? Number(_timestamp) : _timestamp;
		        if (_timestamp > 0) {
		            let d = null;
		            if((_timestamp.toString()).length == 13){
		                d = new Date(_timestamp);
		                
		            }else{
		                d = new Date(_timestamp * 1000);  
		            }
		          
		            let date = (d.getFullYear()) + "-" +
		                sdFormat.string.time((d.getMonth() + 1));
		            return date;
		        } else {
		            return '';
		        }
		    },
		    t2c(_timestamp) {
		        _timestamp = (typeof _timestamp) == 'string' ? Number(_timestamp) : _timestamp;
		        if (_timestamp > 0) {
		            let d = null;
		            if((_timestamp.toString()).length == 13){
		                d = new Date(_timestamp);
		                
		            }else{
		                d = new Date(_timestamp * 1000);  
		            }
		          
		            let date = (d.getFullYear()) + "-" +
		                sdFormat.string.time((d.getMonth() + 1)) + "-" +
		                sdFormat.string.time(d.getDate()) + " " +
		                sdFormat.string.time(d.getHours()) + ":" +
		                sdFormat.string.time(d.getMinutes());
		            return date;
		        } else {
		            return '';
		        }
		    },
		    d2t(date) {
		        let timestamp = Date.parse(new Date(date));
		        return timestamp / 1000;
		    }
		},
		
	}

	exports('sdFormat', sdFormat);
})
